package com.lwj.liwanjia.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.lwj.liwanjia.R;
import com.lwj.liwanjia.bean.GwcBean;
import com.lwj.liwanjia.bean.SelectDpBean;
import com.lwj.liwanjia.config.OnItemClickListener;
import com.lwj.liwanjia.utils.RxBus;
import com.lwj.liwanjia.view.activity.supermarket.ShangpinDetailActivity;
import com.superrecycleview.superlibrary.adapter.BaseViewHolder;
import com.superrecycleview.superlibrary.adapter.SuperBaseAdapter;
import com.superrecycleview.superlibrary.recycleview.SuperRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/9/22.
 */

public class GwAdapter extends SuperBaseAdapter<GwcBean.DataBean> {
    private Context mContext;
    private int sum = 0;//用来判断是否全选中
    private List<GwcBean.DataBean> data;
    private List<Integer> shangpinList = new ArrayList<Integer>();

    public void setSum(int sum) {
        this.sum = sum;
    }

    public GwAdapter(Context context, List<GwcBean.DataBean> data) {
        super(context, data);
        this.mContext = context;
        this.data = data;
    }

    @Override
    protected void convert(BaseViewHolder holder, final GwcBean.DataBean item, final int position) {
        Glide.with(mContext).load(item.getShopInfo().getShopImage()).error(R.mipmap.shop_image_2).into((ImageView)holder.getView(R.id.iv_market));
        holder.setText(R.id.tv_market, item.getShopInfo().getShopName());
        SuperRecyclerView rlv = holder.getView(R.id.rlv_gouwuche);
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        rlv.setRefreshEnabled(false);
        rlv.setLoadMoreEnabled(false);
        rlv.setLayoutManager(manager);
        final List<GwcBean.DataBean.GoodsBean> goods = item.getGoods();
        final ShopAdapter adapter = new ShopAdapter(mContext, goods, position);
        rlv.setAdapter(adapter);
        //选中该店的所有商品
        final CheckBox checkBox = holder.getView(R.id.cb_state1);
        checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<Integer> integers = new ArrayList<Integer>();
                if (checkBox.isChecked()) {
                    sum++;
                    for (int i = 0; i < item.getGoods().size(); i++) {
                        if (item.getGoods().get(i).getShangpinCheck().equals("true")) {
                            integers.add(i);
                        }
                    }
                    RxBus.getInstance().post(new SelectDpBean(position, true, integers, sum));
                    item.getShopInfo().setDianpuCheck("true");
                    notifyDataSetChanged();
                } else {
                    sum--;
                    for (int i = 0; i < item.getGoods().size(); i++) {
                        if (item.getGoods().get(i).getShangpinCheck().equals("false")) {
                            integers.add(i);
                        }
                    }
                    RxBus.getInstance().post(new SelectDpBean(position, false, integers, sum));
                    item.getShopInfo().setDianpuCheck("false");
                    notifyDataSetChanged();
                }
            }
        });
        adapter.setDianpuSum(sum);
        if (item.getShopInfo().getDianpuCheck().equals("true")) {
            checkBox.setChecked(true);
            for (int i = 0; i < goods.size(); i++) {
                goods.get(i).setShangpinCheck("true");
                adapter.setCheckNum(goods.size());
            }

        } else {
            checkBox.setChecked(false);
            shangpinList.clear();
            for (int i = 0; i < goods.size(); i++) {
                if (goods.get(i).getShangpinCheck().equals("true")) {
                    shangpinList.add(i);
                }
                if (shangpinList.size() > 0) {
                    adapter.setCheckNum(shangpinList.size());
                } else {
                    adapter.setCheckNum(0);
                }
            }
            if (shangpinList.size() == goods.size()) {
                for (int i = 0; i < goods.size(); i++) {
                    goods.get(i).setShangpinCheck("false");
                }
            }
        }
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, Object item, int position) {
                Intent intent = new Intent(mContext, ShangpinDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("shopGoodsCode", goods.get(position).getCommodityCode());
                bundle.putString("commodityType", goods.get(position).getCommodityType());
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    protected int getItemViewLayoutId(int position, GwcBean.DataBean item) {
        return R.layout.gouwuche_layout;
    }
}
